﻿<!DOCTYPE xsl:stylesheet [
  <!ENTITY google-analytics-config SYSTEM "google-analytics-config.min.js">
  <!ENTITY clickjaking-protection SYSTEM "clickjaking-protection.min.js">
  <!ENTITY reset-css SYSTEM "reset.min.css">
]>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:exsl="http://exslt.org/common"
  xmlns:model="http://schemas.ad.by/model/2010"
  xmlns:html="http://www.w3.org/1999/xhtml"
  xmlns:v="urn:schemas-microsoft-com:vml"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:svg="http://www.w3.org/2000/svg"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns="http://www.w3.org/1999/xhtml"
  extension-element-prefixes="msxsl exsl"
  exclude-result-prefixes="model html">

  <xsl:output method="xml" indent="no" media-type="text/html" 
    doctype-system="about:legacy-compat" encoding="utf-8" omit-xml-declaration="yes" />
  <xsl:strip-space elements="*" />

  <msxsl:script language="JScript" implements-prefix="exsl">this['node-set'] = function(x){ return x; }</msxsl:script>

  <xsl:param name="server-side-transformation" select="false()" />
  <xsl:param name="production-env" select="true()" />
  <xsl:param name="detect-xslt" select="false()" />
  
  <xsl:variable name="knowyourfonts-cid" />
  <xsl:variable name="browser-check-uri" select="'/xslt/browser-check.xml'" />
  
  <xsl:variable name="models" select="document('/xslt/model.xml')" />

  <xsl:template match="model:feed">
    <xsl:variable name="uri" select="model:link[@rel = 'self']/@href" />
    <xsl:variable name="model" select="$models/model:root/model:page[@href = $uri]" />
    
    <xsl:variable name="title" select="$model/@title" />
    <xsl:variable name="description" select="$model/@subtitle" />
    <xsl:variable name="author" select="model:author/model:uri" />
    <xsl:variable name="atom-feed" select="model:link[@rel = 'alternate' and @type = 'application/atom+xml']" />
    
    <xsl:variable name="do-browser-check" select="model:link[@href = $browser-check-uri]" />
    <xsl:variable name="kyf-enabled" select="$production-env and not($do-browser-check)" />
    
    <xsl:variable name="css">
      <xsl:apply-templates select="$model" mode="css" />
    </xsl:variable>
    <xsl:variable name="body">
      <xsl:apply-templates select="$model" />
    </xsl:variable>
    <xsl:variable name="script">
      <xsl:apply-templates select="$model" mode="script" />
    </xsl:variable>

    <html>
      <xsl:attribute name="class" >
        <!-- mark IE version -->
        <xsl:if test="model:user-agent/@is-ie and model:user-agent/@version-major">
          <xsl:value-of select="concat(' ie', model:user-agent/@version-major)" />
        </xsl:if>
      </xsl:attribute>
      
      <!-- adding the ability to be saved to a disk and then opened in IE without warnings -->
      <xsl:comment>
        <xsl:value-of select="concat(' saved from url=(00', string-length($uri), ')', $uri, ' ')" />
      </xsl:comment>

      <head>
        <title>
          <xsl:value-of select="$title" />
        </title>

        <!-- no compatibility button in IE -->
        <!-- http://msdn.microsoft.com/en-us/library/cc288325(v=vs.85).aspx -->
        <!-- The X-UA-Compatible header is not case sensitive; however, it must appear in the header of the webpage (the HEAD section) before all other elements except for the title element and other meta elements. -->
        <meta http-equiv="X-UA-Compatible" content="IE=Edge" />

        <!-- http://diveintohtml5.org/semantics.html#encoding -->
        <!-- http://lists.w3.org/Archives/Public/public-html/2007Jul/0550.html -->
        <!-- <xsl:variable name="xsl-output" select="document('')/xsl:stylesheet/xsl:output"/> --> 
        <!-- <meta charset="{$xsl-output/@encoding}" /> -->
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <!-- mark user agent as XSLT-possible -->
        <xsl:if test="not($server-side-transformation)">
          <meta http-equiv="Set-Cookie" content="can-xslt=1; path=/; " />
        </xsl:if>
        
        <!-- no IE toolbar for big images (200x200) http://support.microsoft.com/kb/303473 -->
        <meta http-equiv="imagetoolbar" content="no" />

        <!-- Mobile viewport optimized: https://github.com/paulirish/html5-boilerplate/issues/issue/37 -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <!-- autoring and copyright information -->
        <xsl:if test="$author">
          <meta name="author" content="{$author}" />
        </xsl:if>

        <!-- description, css, script we put at the end of head -->
        <xsl:if test="$description">
          <meta name="description" content="{$description}" />
        </xsl:if>

        <!-- site icon support -->
        <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <!-- Android 2.2+ & IPhone icon support (should be 57x57 pixels) -->
        <!-- http://www.ravelrumba.com/blog/android-apple-touch-icon/ -->
        <!-- http://mathiasbynens.be/notes/touch-icons -->
        <link href="/apple-touch-icon.png" rel="apple-touch-icon" />

        <!-- 1. http://www.google.com/support/webmasters/bin/answer.py?answer=139394 -->
        <!-- 2. A google +1 button uses it also -->
        <link href="{model:id}" rel="canonical" />
        
        <!-- RSS/Atom support -->
        <xsl:if test="$atom-feed">
          <link href="{$atom-feed}" title="RSS/Atom Feed" type="application/atom+xml" rel="alternate" />
        </xsl:if>

        <!-- css -->
        <xsl:variable name="all-css">
          <xsl:copy-of select="$css" />
        </xsl:variable>

        <style type="text/css">
          <xsl:text>&reset-css;</xsl:text>
          <xsl:value-of select="normalize-space(exsl:node-set($all-css))" />
        </style>

        <!-- insert minified css with no duplicates -->
        <!--
        <xsl:call-template name="minify-css">
          <xsl:with-param name="css">
            <xsl:apply-templates select="exsl:node-set($all-css)" mode="css-distinct" />
          </xsl:with-param>
        </xsl:call-template>
        -->

        <xsl:if test="not(model:user-agent/@noscript)">
	        <script type="text/javascript">
	          <!-- 1. Simple ClickJacking protection -->
	          <!-- http://code.google.com/p/browsersec/wiki/Part2#Arbitrary_page_mashups_(UI_redressing) -->
	          <!-- http://how.appspot.com/frame -->
	          <!-- http://en.wikipedia.org/wiki/Framekiller -->
	          <!-- http://stackoverflow.com/questions/958997/frame-buster-buster-buster-code-needed -->
	          <!-- http://coderrr.wordpress.com/2009/02/13/preventing-frame-busting-and-click-jacking-ui-redressing/ --> 
	          <!-- http://coderrr.wordpress.com/2009/06/18/anti-anti-frame-busting/ -->
	          <!-- "top!=self" will detect even static binding to an alien IFRAME -->
	          <!-- "top.location" can be accessible from sameorigin domain, otherwise exception -->
	          <!-- see twitter.com page source -->
	          <xsl:if test="not($do-browser-check)">
	            <xsl:text>&clickjaking-protection;</xsl:text>
	          </xsl:if>
	          
	          <!-- 2. quirks mode detect -->
	          <!-- http://stackoverflow.com/questions/1493386/how-to-detect-quirks-mode-in-safari-3-0-x -->
	          <xsl:text><![CDATA[if(!document.compatMode){var a,b=((a=document.createElement("p").style).width="1",!a.width);document.compatMode=b?"CSS1Compat":"BackCompat"};]]></xsl:text>
	          <xsl:text><![CDATA[if(document.documentMode==5||document.compatMode=="BackCompat")document.documentElement.className+=" quirks";]]></xsl:text>
	          
	          <!-- 3. Firefox body tag fix (http://ajaxandxml.blogspot.com/2008/10/firefox-forgets-to-create-documentbody.html) -->
	          <xsl:if test="not($server-side-transformation)">
	            <xsl:text><![CDATA[if(document.addEventListener)document.addEventListener("DOMContentLoaded",function(){if(!document.body)document.body=document.getElementById("body")},false);]]></xsl:text>
	          </xsl:if>
	          
	          <!-- 4. Mark user agent as XSLT-possible - Firefox do not set cookies from meta after client-side xslt -->
	          <xsl:if test="not($server-side-transformation)">
	            <xsl:text><![CDATA[document.cookie="can-xslt=1; path=/; ";]]></xsl:text>
	          </xsl:if>
	          
	          <!-- 5. Google Analytics config -->
	          <xsl:if test="$production-env">
	            <xsl:text>&google-analytics-config;</xsl:text>
	          </xsl:if>
	          
	          <!-- 6. Timezone detect -->
	          <!-- temporarily not needed -->
	          <!-- <xsl:text><![CDATA[document.cookie="ts="+-1*(new Date(Date.UTC(2010,6,30,0,0,0,0))).getTimezoneOffset()+"; path=/; ";document.cookie="tw="+-1*(new Date(Date.UTC(2010,12,30,0,0,0,0))).getTimezoneOffset()+"; path=/; ";]]></xsl:text> -->
	        </script>
	        
          <!-- 7. Detect no JavaScript -->
          <noscript>
            <meta http-equiv="refresh" content="0; URL=/?noscript=1" />
          </noscript>
        </xsl:if>
      </head>

      <!-- do not delete the ID="body" - Firefox body tag fix (http://ajaxandxml.blogspot.com/2008/10/firefox-forgets-to-create-documentbody.html) -->
      <body id="body">
        <xsl:copy-of select="$body" />

        <xsl:if test="not(model:user-agent/@noscript)">
	        <!-- jquery -->
	        <xsl:call-template name="jquery-script" />
	
	        <!-- insert js with no duplicates -->
	        <script type="text/javascript">
	          <xsl:value-of select="normalize-space(exsl:node-set($script))" />
	        </script>
	        <!--
	        <xsl:apply-templates select="exsl:node-set($script)" mode="script-distinct" />
	        -->
        </xsl:if>

        <!-- opera mini has serious bug - it hides the page content when the iframe with xml has found -->
        <xsl:if test="$detect-xslt and $server-side-transformation and not(model:user-agent/@is-opera-mini)">
          <iframe src="{$browser-check-uri}" style="height: 0px; width: 0px;" />
        </xsl:if>
        
        <!-- KnowYourFonts -->
        <xsl:if test="$kyf-enabled">
	        <xsl:variable name="kyf-server">
	          <xsl:if test="$production-env">//knowyourfonts.appspot.com</xsl:if>
	        </xsl:variable>
        
          <!--
          <noscript>
          classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" 
          data="{$kyf-server}/kyf.swf" 
          -->
            <object width="1" height="1" type="application/x-shockwave-flash" style="position: absolute; top: 1px; left: 1px;">
              <param name="movie" value="{$kyf-server}/kyf.swf" />
              <param name="allowScriptAccess" value="always" />
              <param name="allowNetworking" value="all" />
              <param name="wmode" value="transparent" />
              <!-- FSCommand - http://kb2.adobe.com/cps/127/tn_12701.html -->
              <!--param name="swliveconnect" value="true" /-->
              <xsl:if test="$knowyourfonts-cid"> 
                <param name="FlashVars" value="cid={$knowyourfonts-cid}" />
              </xsl:if>
            </object>
          <!--
          </noscript>
          -->
        </xsl:if>
        
        <xsl:if test="model:user-agent/@noscript and model:google-analytics-image-url">
          <img src="{model:google-analytics-image-url}" height="1" width="1" />
        </xsl:if>
      </body>
    </html>
  </xsl:template>

  <!-- override default rules for #text output -->
  <xsl:template match="text()" mode="css" />
  <xsl:template match="text()" mode="script" />

  <!-- no root/#style processing for body -->
  <xsl:template match="html:style" />

  <!-- root/#style processing for 'css' mode -->
  <xsl:template match="html:style" mode="css">
    <xsl:copy>
      <xsl:apply-templates select="@*" mode="css" />
      <xsl:value-of select="normalize-space()" />
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="@*" mode="css">
    <xsl:copy-of select="." />
  </xsl:template>

  <!-- no root/#script processing for body -->
  <xsl:template match="html:script" />
  
  <!-- root/#script processing for 'script' mode -->
  <xsl:template match="html:script" mode="script">
    <xsl:copy-of select="." />
  </xsl:template>

  <!-- remove duplicates in CSS -->
  
  <xsl:template match="html:link[@href]" mode="css-distinct">
    <xsl:if test="not(preceding-sibling::html:link[@href = current()/@href])">
      <xsl:copy-of select="."/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="html:style" mode="css-distinct">
    <xsl:if test="not(preceding-sibling::html:style[text() = current()/text()])">
      <xsl:copy-of select="." />
    </xsl:if>
  </xsl:template>

  <!-- remove duplicates in JS -->

  <xsl:template match="html:script[@src]" mode="script-distinct">
    <xsl:if test="not(preceding-sibling::html:script[@src = current()/@src])">
      <xsl:copy-of select="." />
    </xsl:if>
  </xsl:template>

  <xsl:template match="html:script[not(@src)]" mode="script-distinct">
    <xsl:if test="not(preceding-sibling::html:script[text() = current()/text()])">
      <xsl:copy-of select="." />
    </xsl:if>
  </xsl:template>

  <!-- minify-css -->

  <xsl:template name="minify-css">
    <xsl:param name="css" />
    <xsl:apply-templates select="exsl:node-set($css)/*" mode="minify-css" />
  </xsl:template>

  <xsl:template match="html:link" mode="minify-css">
    <xsl:copy-of select="." />
  </xsl:template>
  
  <xsl:template match="html:style" mode="minify-css">
    <xsl:copy>
      <xsl:apply-templates select="@*" mode="minify-css" />
      <xsl:value-of select="normalize-space()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@*" mode="minify-css">
    <xsl:copy-of select="." />
  </xsl:template>

  <!-- utils -->
  
  <xsl:template name="jquery-script">
    <!-- empty -->
  </xsl:template>

  <xsl:template name="jquery-google-analytics">
    <!-- empty -->
  </xsl:template>
  
</xsl:stylesheet>